<?php
namespace Home\Controller;
use Common\Common\BaseController;
use Model\TagModel;
use Think\Controller;
use Model\ArticleModel;
use Think\Log;

/**
 * Created by PhpStorm.
 * User: Yujie
 * Date: 2018/8/31
 * Time: 20:03
 */
class ArticleController extends BaseController{

    private $_db_art;
    private $_db_tag;
    public function __construct()
    {
        parent::__construct();
        $this->_db_art  = new ArticleModel();
        $this->_db_tag  = new TagModel();
    }

    /**
     * @api 文章列表
     */
    public function articleList(){
        $p          = I('post.p',1);
        $p_size     = I('post.p_size',5);
        $mo         = I('post.mo',0);

        $r_data['article'] = $this->artList($p,$p_size,$mo);
        $r_data['pagination'] = $this->pagination($p,$mo);
        $r_data['tag']  = $this->getTags();

        $this->ajaxReturn($r_data,'json');
    }

    /**
     * @function 获取热门标签
     */
    private function getTags(){
        $ret = $this->_db_tag->selectAll();
        if(!empty($ret)){
            foreach($ret as $k=>$v){
                $ret[$k]['href'] = U('Home/index/index/mo/'.$v['id']);
            }
        }
        return $ret;
    }

    /**
     * @function 文章列表
     * @param $p 位移
     * @param $p_size  单页显示数目
     */
    private function artList($p,$p_size=5,$mo=0){
        $w_art = [];
        if($mo){
            $w_art['tag_id'] = $mo;
        }
        $ret_article = $this->_db_art->selectAll($w_art,'*',($p-1)*$p_size,$p_size);


        // 给第一个文章添加url
        foreach($ret_article as $k =>$v){
            $ret_article[$k]['href'] = U('Home/Article/articleDetail/article_id/'.$v['id']);
        }
        return $ret_article;
    }

    /**
     * @api 文章详情
     */
    public function articleDetail(){
        $art_id = I('get.article_id');
        if(!$art_id){
            $this->errorView(201,'无效信息');
        }
        $db = new ArticleModel();

        $w['id']    = $art_id;
        $ret = $db->findOne($w);
        $this->assign('title',$ret['title']);
        $this->assign('info',$ret);
        $this->display('Article/articleDetail');
    }


    /**
     * @funciton 分页
     * @param $data 当页数据
     * @param int $p 第N页
     * @return string html
     */
    private function pagination($p=1,$mo=0){
        $w['id'] = ['gt',0];
        $b_url = 'Home/index/index/';
        if($mo){
            $w['tag_id'] = $mo;
            $b_url .= 'mo/'.$mo.'/p/';
        }else{
            $b_url .= '/p/';
        }
        $ret = (new ArticleModel())->findOne($w,'count(1) as num');

        $pages = intval($ret['num']/5);
        if($ret['num']%5 > 0){
            $pages +=1;
        }
        $str='';
        if($p != 1){
            $pre = $p-1;
            $str = '<a href="'.U($b_url.$pre).'" class="next">&lt;&lt;上一页</a>';
        }
        for($i=1;$i<=$pages;$i++){
            if($i==$p){
                $str .= '<span>'.$i.'</span>';
            }else{
                $str .= '<a href="'.U($b_url. $i ).'" hidefocus="">'.$i. '</a>';
            }
        }
        if($p<$pages){
            $next = $p+1;
            $str .= '<a href="'.U($b_url.$next  ).'" class="next">下一页&gt;&gt;</a>';
        }

        return $str;
    }
}